package com.sctvcloud.renhe.ui.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.IdRes;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ruihang.generalibrary.ui.dialog.BaseDialogFragment;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.SharedPreferencesUtil;
import com.sctvcloud.renhe.R;
import com.sctvcloud.renhe.application.Constances;

import static com.sctvcloud.renhe.application.Constances.MID_FONT;
import static com.sctvcloud.renhe.application.Constances.SMALL_FONT;

/**
 * Created by Administrator on 2017/6/20 0020.
 */

public class FontSizeDiaFragment extends BaseDialogFragment implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
    private RadioGroup font_rg;
    private RadioButton big_rb, mid_rb, small_rb;
    private CustomFontTextView preview_cv;

    private boolean isIniting = true;

    @Override
    protected int getContentRes() {
        return R.layout.dia_font_size_fragment;
    }

    @Override
    protected void initViews(Dialog dialog, Bundle savedInstanceState) {
        font_rg = (RadioGroup) dialog.findViewById(R.id.dia_font_choice_rg);
        big_rb = (RadioButton) dialog.findViewById(R.id.dia_font_big_rb);
        mid_rb = (RadioButton) dialog.findViewById(R.id.dia_font_mid_rb);
        small_rb = (RadioButton) dialog.findViewById(R.id.dia_font_small_rb);
        preview_cv = (CustomFontTextView) dialog.findViewById(R.id.dia_font_font_preview);
        preview_cv.setOnClickListener(this);
        font_rg.setOnCheckedChangeListener(this);
        // 缓存字体大小
        int fontSize = (int) SharedPreferencesUtil.getParam(getActivity(), Constances.FONT_KEY, MID_FONT);
        if (Constances.BIG_FONT == fontSize) {
            big_rb.setChecked(true);
        } else if (MID_FONT == fontSize) {
            mid_rb.setChecked(true);
        } else if (SMALL_FONT == fontSize) {
            small_rb.setChecked(true);
        }
    }


    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.dia_font_big_rb:
                choice(big_rb, mid_rb, small_rb, Constances.BIG_FONT);
                preview_cv.setTextSize(Constances.BIG_FONT);
                break;
            case R.id.dia_font_mid_rb:
                choice(mid_rb, big_rb, small_rb, MID_FONT);
                preview_cv.setTextSize(MID_FONT);
                break;
            case R.id.dia_font_small_rb:
                choice(small_rb, big_rb, mid_rb, SMALL_FONT);
                preview_cv.setTextSize(SMALL_FONT);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        if (v == preview_cv) {
            // toast(getString(R.string.font_preview));
        }
    }

    private void choice(RadioButton rb1, RadioButton rb2, RadioButton rb3, int size) {
        rb1.setBackgroundResource(R.drawable.font_size_circle_choice);
        rb1.setTextColor(getResources().getColor(R.color.white));
        rb2.setBackgroundResource(R.drawable.font_size_circle_no_choice);
        rb2.setTextColor(getResources().getColor(R.color.colorBlack22));
        rb3.setBackgroundResource(R.drawable.font_size_circle_no_choice);
        rb3.setTextColor(getResources().getColor(R.color.colorBlack22));
        // 缓存字体大小
        SharedPreferencesUtil.setParam(getActivity(), Constances.FONT_KEY, size);
        if (isIniting) {
            isIniting = false;
            return;
        }
        if (callBack != null) {
            Message message = new Message();
            message.what = size;
            callBack.onCallBackMsg(this, message);
        }
    }
}
